<template>
	<view class="container">
		<view class="mainMid">
			<view class="login-panel">
				<view class="uni-form-item uni-column">
					<image src="/static/img/user-icon.png"></image> 
					<input class="uni-input" @blur="onblur" name="mobile" type="number" @input="handleInputMobile" placeholder-style="font-size:26upx"
					 v-model="formData.mobile" placeholder="请输入手机号" />
				</view>
				<view class="uni-form-item uni-column">
					<image src="/static/img/password-icon.png"></image>
					<input class="uni-input" @blur="onblur" name="password" @input="handleInputPassword" :password="true"
					 placeholder-style="font-size:26upx" v-model="formData.password" placeholder="请输入密码" />
				</view>
			</view>
			<view class="login-btn" :class="{active:formData.mobile && formData.password || isMobile && isPassword}" @click="login">
				注册
			</view>
		</view>
	</view>
</template>

<script>
	import {
		requestAll
	} from '@/http/api'
	import graceChecker from "@/util/graceChecker.js"
	export default {
		data() {
			return {
				checked: false,
				isMobile: false,
				isPassword: false,
				formData: {},
				remember: {}
			}
		},
		onShow() {
			uni.getStorage({
				key: 'remember',
				success: res => {
					console.log(res);
					this.$set(this.formData, 'mobile', res.data.mobile);
					this.$set(this.formData, 'password', res.data.password);
					this.checked = res.data.checked;
				}
			})
		},
		onLoad(e) {
      this.$set(this.formData,'mobile',e.phone)
		},
		mounted() {

		},
		methods: {
			onblur() {
				setTimeout(function() {
					uni.pageScrollTo({
						scrollTop: 0
					})
				}, 100);
			},
			handleInputMobile(val) { // 判断手机框里是否有值
				if (val.detail.value) {
					this.isMobile = true
				} else {
					this.isMobile = false
				}
			},
			handleInputPassword(val) { // 判断密码框里是否有值
				if (val.detail.value) {
					this.isPassword = true
				} else {
					this.isPassword = false
				}
			},
			handleLogin() {
				uni.redirectTo({
					url: "./req"
				})
			},
			login() {
				var rule = [{
						name: "mobile",
						checkType: "phoneno",
						errorMsg: "请输入正确的手机号"
					},
					{
						name: "password",
						checkType: "reg",
						checkRule: "(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}",
						errorMsg: "密码最少6位且必须要同时包含数字和字母！"
					}
				];
				var checkRes = graceChecker.check(this.formData, rule);
				if (checkRes) {
					this.formData.random_str = this.$dokey.getRandom(); // 字符串
					this.formData.timestamp = this.$dokey.getTime(); // 时间戳
					this.formData.method = 'user.wx.reg'; // 路径
          this.formData.openid = uni.getStorageSync('wxAuth').openid
					this.formData.sign = this.$md5(this.$dokey.secretdate('user.wx.reg'));
					this.formData.nickname = uni.getStorageSync('userInfo').nickName
					this.formData.avatar = uni.getStorageSync('userInfo').avatarUrl
					console.log(this.formData)
					requestAll(this.formData).then(resp => {
						if (resp.response_data.user_data) { // 这里判断是否已经是注册且登陆状态，如果是获取token ，将数据存到myInfo中
						  uni.setStorageSync('token', resp.response_data.user_data.token)
						  uni.setStorage({
						    key: 'myInfo',
						    data: resp.response_data.user_data,
						    success: (res1) => {
						      uni.redirectTo({
						        url: "/pages/my/my"
						      })
						    }
						  })           
						}
					}).catch(err => {
						console.log(err);
					})
				} else {
					uni.showToast({
						title: graceChecker.error,
						icon: "none"
					});
				}
			}
		}
	}
</script>

<style scoped lang="scss">

	.mainMid {
		position: relative;
		z-index: 1;
	}

	.login-panel {
		width: 710upx;
		height: 400upx;
		margin: 33upx auto 0;
		padding: 56upx;
		box-sizing: border-box;
		background: rgba(255, 255, 255, 1);
		border-radius: 34upx;
		box-shadow: 0 0 10upx rgba(0, 0, 0, 0.2);
	}

	.uni-form-item {
		position: relative;
		display: flex;
		padding: 34upx 0;
		border-bottom: 1px solid #B5B4B4;
		image {
			width: 33rpx;
			height: 33rpx;
		}
		input {
			flex: 1;
		}
	}

	.uni-form-item text {
		position: absolute;
		font-size: 36upx;
		color: #1334D5;
	}

	.uni-input {
		padding: 0 33upx;
		font-size: 26upx;
		color: #868686;
	}

	.send {
		position: absolute;
		top: 50%;
		right: 0;
		width: 197upx;
		height: 58upx;
		transform: translateY(-50%);
		line-height: 58upx;
		border-radius: 29upx;
		/* background: #ccc; */
		background: #007AFF;
		font-size: 30upx;
		font-family: Adobe Heiti Std;
		font-weight: normal;
		color: rgba(255, 255, 255, 1);
		text-align: center;
	}

	.login-btn {
		width: 550upx;
		height: 90upx;
		margin: -47upx auto 0;
		line-height: 90upx;
		border-radius: 47upx;
		background: #ddd;
		text-align: center;
		font-size: 36upx;
		font-family: Adobe Heiti Std;
		font-weight: normal;
		color: rgba(255, 255, 255, 1);
	}

	.login-btn.active {
		background: #007AFF;
	}

	.forgetTips {
		padding: 10upx 0;
		text-align: center;
		color: #0000FF;
		font-size: 24upx;
	}

	.remember {
		padding: 5upx 0;
		font-size: 28upx;
		color: grey;
	}

	.remember checkbox {
		transform: scale(0.7);
	}
</style>
